<?php
/*
 ReadThis Template Name: NeoLao
 ReadThis Template URI: http://resources.neolao.com/flash/components/player_mp3/templates/maxi
 Description: an adaptation for the NeoLao MP3 Player
 Version: 1
 Author: XoraX
 Author URI: http://www.xorax.info/
*/

class readthis_template_abstract {
  var $swf; //the swf file will be defined in the constructor() (because, in a futur version, a preview of it will be screen in plugin page)
  var $inline; //is template is used for display inline of posts ?
  var $widget; //is template is used for display a widget ?
  var $width;
  var $height;
  
  function readthis_template_abstract ($tpldirurl, $inline, $widget){
    //$this->swf = trailingslashit(get_option('siteurl')).trailingslashit(PLUGINDIR).'readthis/template/'.str_replace('readthis_template_','',get_class($this)).'.swf';
    $this->swf = $tpldirurl.substr(get_class($this),18).'.swf';
    $this->swf = $tpldirurl.'player_mp3_maxi_0.5.0.swf';
    $this->inline = $inline;
    $this->widget = $widget;
    $this->width = 180;
    $this->height = 20;
    if($inline && $widget) add_action('wp_head',array(&$this, 'js_head'));
  }
  
  function object($params = array(), $post = null){
    $audio = get_the_audio_url(false, $post);
    if(!$audio) return false;
    if(!is_array($params)) $params = array();
    
    $def_params = array( //default params of the swf override by the function parameters
      'mp3' => $audio,
      //'showinfo' => 1,
      'showvolume' => 1,
      'width' => $this->width,
      'height' => $this->height,
      'buttoncolor' => '7777DD',
      'buttonovercolor' => '9999FF',
      'slidercolor1' => '4444AA',
      'slidercolor2' => '9999CC',
      'sliderovercolor' => '9999CC',
      //'bgcolor' => '666666'
    );
    $params = array_merge($def_params, $params);
    return array(
      'swf' => $this->swf,
      'params' => $params,
    );
  }
  
  function html_object($params = array(), $id = false, $post = null){
    $params = $this->object($params,$post);
    if(!$params) return '';
    
    $params = $params['params'];
    $params_url = '';
    foreach($params as $p => $v){
      $params_url .= rawurlencode($p).'='.rawurlencode($v).'&';
    }
    $name = ($id) ? ' name="'.$id.'"':'';
    $id = ($id) ? ' id="'.$id.'"':'';
    return
      '<object width="'.$this->width.'" height="'.$this->height.'" data="'.$this->swf.'" type="application/x-shockwave-flash"'.$id.'>'.
        '<param name="movie" value="'.$this->swf.'"/>'.
        '<param name="FlashVars" value="'.$params_url.'"/>' .
        '<param name="wmode" value="transparent" />'.
        '<embed width="'.$this->width.'" height="'.$this->height.'"'.$name.' '.
          'pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" ' .
          'allowscriptaccess="sameDomain" quality="high" wmode="transparent" ' .
          'src="'.$this->swf.'" FlashVars="'.$params_url.'" swLiveConnect="true"></embed>'.
      '</object>';
  }
  
  function js_head(){
    echo '
    <script type="text/javascript">
    var sendReadThis = function(url,id){
      var f = document.getElementById("'.get_class($this).'") || window.document.'.get_class($this).';
      if(id == sendReadThis.oldid) f.SetVariable("player:jsStop", "");
      else {
        f.SetVariable("player:jsUrl", url);
        f.SetVariable("player:jsPlay", "");
      }
      if(id){
        if(sendReadThis.oldid != id) document.getElementById("ReadThisCall_"+id).innerHTML = "stop";
        if(sendReadThis.oldid) document.getElementById("ReadThisCall_"+sendReadThis.oldid).innerHTML = "Play";
        sendReadThis.oldid = id;
      }
    }
    </script>';
  }
  
  function inline(&$content){
    if($this->widget){
      global $post;
      $inline = '<a id="ReadThisCall_'.$post->ID.'" href="javascript:sendReadThis(\''.get_the_audio_url().'\','.$post->ID.');">Play</a>';
    } else {
      $inline = $this->html_object();
      $inline = ($inline) ? '<div style="z-index:-100;">'.$inline.'</div>' : '';
    }
    return $inline.$content;
  }
  
  function widget($args) {
    extract($args);
    ?>
          <?php echo $before_widget; ?>
              <?php /*echo $before_title
                  . 'My ReadThis Widget'
                  . $after_title; ?>
              <?php /**/
              echo $this->html_object(array(),get_class($this));
              if(!is_single() || !is_page()){
                while (have_posts()) { the_post();
                  echo '<br/>'.'<a href="javascript:sendReadThis(\''.get_the_audio_url().'\','.$GLOBALS['post']->ID.');">'.get_the_title().'</a>';
                }
              }
              ?>
          <?php echo $after_widget; ?>
    <?php
  }
  
  function widget_control(){
    // Get our options and see if we're handling a form submission.
    ?><div style="background-color: #FFFF00; width:400px; height: 50px;"> </div><?php
    $options = get_option('widget_jfa_PluginsUsedPlugin');
    if (!is_array($options)) {
      $options = array('title'=>'');
    }
    
    if ($_POST['jfa_PluginsUsedPlugin_submit']) {
      // Remember to sanitize and format use input appropriately.
      $options['title'] = strip_tags(stripslashes($_POST['jfa_PluginsUsedPlugin_title']));
      update_option('widget_jfa_PluginsUsedPlugin', $options);
    }

    // Be sure you format your options to be valid HTML attributes.
    $title = htmlspecialchars($options['title'], ENT_QUOTES);
    
    // Here is our little form segment. Notice that we don't need a
    // complete form. This will be embedded into the existing form.
    echo '<p style="text-align:right;"><label for="jfa_PluginsUsedPlugin_title">Title: <input style="width: 200px;" id="jfa_PluginsUsedPlugin_title" name="jfa_PluginsUsedPlugin_title" type="text" value="'.$title.'" /></label></p>';
    echo '<input type="hidden" id="jfa_PluginsUsedPlugin_submit" name="jfa_PluginsUsedPlugin_submit" value="1" />';
  }
}

?>
